package woaini.fenger.core.task.system;

import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import woaini.fenger.core.bot.base.Bot;
import woaini.fenger.core.bot.enums.BotStatusEnum;
import woaini.fenger.core.bot.manager.BotManager;
import woaini.fenger.core.task.anno.BotTask;

import java.util.Collection;

@Component
@Slf4j
@DisallowConcurrentExecution
@BotTask(name = "心跳")
public class BotHeartTask extends QuartzJobBean {

    @Autowired
    BotManager botManager;

    @Override
    protected void executeInternal(JobExecutionContext context){
        Collection<Bot> bots = botManager.getBots();
        for (Bot bot : bots) {
            BotStatusEnum status = bot.getStatus();
            if (!status.equals(BotStatusEnum.OFFLINE)){
                continue;
            }
            bot.reConncet();
            log.info("bot离线，尝试重连:{}", bot.getKey());
        }
    }
}
